import logging


def setup_logger(logger_name, log_file, level=logging.INFO):
    logger = logging.getLogger(logger_name)
    formatter = logging.Formatter('%(asctime)-2s %(levelname)-8s %(message)s')
    fileHandler = logging.FileHandler(log_file, mode='w')
    fileHandler.setFormatter(formatter)
    streamHandler = logging.StreamHandler()
    streamHandler.setFormatter(formatter)

    logger.setLevel(level)
    logger.addHandler(fileHandler)
    logger.addHandler(streamHandler)


class LogUtils(object):
    def __init__(self,level_names=['info','debug','error','warning']):
        self.info_logger = False
        self.debug_logger = False
        self.error_logger = False
        self.warning_logger = False
        if 'info' in level_names:
            setup_logger('info', "log/log_info.log", logging.INFO)
            self.info_logger = logging.getLogger('info')
        if 'debug' in level_names:
            setup_logger('debug', "log/log_debug.log", logging.DEBUG)
            self.debug_logger = logging.getLogger('debug')
        if 'error' in level_names:
            setup_logger('error', "log/log_error.log", logging.ERROR)
            self.error_logger = logging.getLogger('error')
        if 'warning' in level_names:
            setup_logger('warning', "log/log_warning.log", logging.WARNING)
            self.warning_logger = logging.getLogger('warning')

    def info(self, msg):
        if self.info_logger:
            self.info_logger.info(msg)
        pass

    def debug(self, msg):
        if self.debug_logger:
            self.debug_logger.debug(msg)
        pass

    def warning(self, msg):
        if self.warning_logger:
            self.warning_logger.warning(msg)
        pass

    def error(self, msg):
        if self.error_logger:
            self.error_logger.error(msg)
        pass

